import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class Modify extends JDialog implements ActionListener {
    JLabel infor;
    JTextField inputNumber;
    Object name[] = {"编号", "书名", "出版社", "出版日期"};
    Object a[][] = new Object[1][4];
    JTable table;
    JButton queren;
    Connection con;
    Statement sql;
    ResultSet rs;
    String num;

    Modify() {
        infor = new JLabel("输入编号(以回车确认)：");
        inputNumber = new JTextField(20);
        table = new JTable(a, name);
        queren = new JButton("修改记录");
        setLayout(new FlowLayout());
        infor.setPreferredSize(new Dimension(100, 70));
        add(infor);
        add(inputNumber);
        add(table);
        queren.setPreferredSize(new Dimension(80, 50));
        add(queren);
        inputNumber.addActionListener(this);
        queren.addActionListener(this);
        setBounds(20, 60, 300, 500);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ee) {
            System.out.println("" + ee);
        }
        if (e.getSource() == inputNumber) {
            try {
                num = inputNumber.getText().trim();
                con = DriverManager.getConnection(Main.DB_URL, Main.DB_USER_NAME, Main.DB_PASSWORD);
                sql = con.createStatement();
                rs = sql.executeQuery("SELECT * FROM insertBook WHERE 编号='" + num + "'");
                boolean boo = rs.next();
                if (boo == false) {
                    JOptionPane.showMessageDialog(this, "编号不存在", "提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    a[0][0] = rs.getString(2);
                    a[0][1] = rs.getString(3);
                    a[0][2] = rs.getString(4);
                    table.repaint();
                }
                con.close();
            } catch (SQLException eee) {
                System.out.println(eee);
            }
        }
        if (e.getSource() == queren) {
            try {
                table.repaint();
                con = DriverManager.getConnection(Main.DB_URL, Main.DB_USER_NAME, Main.DB_PASSWORD);
                sql = con.createStatement();
                int k = sql.executeUpdate("UPDATE insertBook SET 书名='" + a[0][0] + "',出版社='" + a[0][1] + "',出版日期 ='" + a[0][2] + "' WHERE 编号='" + num + "'");
                if (k == 1) {
                    JOptionPane.showMessageDialog(this, "更新成功", "成功", JOptionPane.PLAIN_MESSAGE);
                    con.close();
                    setVisible(false);
                }
            } catch (SQLException eeee) {
                JOptionPane.showMessageDialog(this, "更新失败" + eeee, "失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
